package com.jiadakeji.ppm.common.coderule;


import com.jiadakeji.ppm.common.utils.IdGen;
import com.jiadakeji.ppm.common.utils.SpringContextHolder;
import com.jiadakeji.ppm.modules.business.dao.FeeReceiveDao;
import com.jiadakeji.ppm.modules.business.entity.FeeReceive;

import java.time.LocalDate;
import java.util.HashMap;

/**
 * 水厂编码生成
 *
 */
public class CodeRuleFeeReceive implements CodeRule {

    /**
     *
     * null
     * @return 一位随机编码
     */
    @Override
    public String generateCode(HashMap<String, String> paras) {


        String day = LocalDate.now().toString().replaceAll("-","");
        String prex = day;
        String code = "1"+prex+getCode();
        while (! codeNoExist(code)){
            code = prex+getCode();
        }

        return code;
    }

    @Override
    public String generateCode() {


        String day = LocalDate.now().toString().replaceAll("-","");
        String prex = day;
        String code = "1"+prex+getCode();
        while (! codeNoExist(code)){
            code = prex+getCode();
        }

        return code;
    }
    @Override
    public String getCode() {
        return IdGen.generaChar(8);
    }

    @Override
    public boolean codeNoExist(String code) {
       FeeReceiveDao dao = SpringContextHolder.getBean(FeeReceiveDao.class);
         FeeReceive feeReceive = dao.getByCode(code);
        if(null == feeReceive){
            return true;
        }else{
            return false;
        }
    }
}
